<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/WEB-INF/conf/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/conf/tld/struts-tiles.tld" prefix="tiles"%>
<%@ taglib uri="/WEB-INF/conf/tld/struts-logic.tld" prefix="logic"%> 
<%@ taglib uri="/WEB-INF/conf/tld/struts-bean.tld" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0043)http://ostermiller.org/calc/calculator.html -->
<HTML>
<HEAD>

<TITLE>计算器</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<style type="text/css">

.button-style{
border-left:1px solid #999999;
border-top:1px solid #999999;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
width:34px;
height:24px;
color:#fff;
font-weight:bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
background:#13AEDE;
input{border:1px none #fff;}
}

.button-style2{
border-left:1px solid #999999;
border-top:1px solid #999999;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
width:44px;
height:24px;
color:#000;
font-weight:bold;
font-family:Verdana, Arial, Helvetica, sans-serif;
background:#FCD03B;
}
.blue-bg{
background:#E2E4E6;
border:1px solid #000;
}
</style>
</HEAD>

<!-- <body bgcolor="#ffffff" onLoad="FKeyPad.ReadOut.focus();FKeyPad.ReadOut.select();">-->
<body bgcolor="#ffffff" style="overflow:hidden">
<FORM name="Keypad" action="">
 <B>
 <TABLE align="center" border=2 width=50 cellpadding=1 cellspacing=5 class="blue-bg">
  <TR>
   <TD colspan=3 align=middle><input name="ReadOut" type="Text" onKeyPress="CheckOut()" size=24 value="0" width=100%></TD>
   <TD></TD>
   <TD><input name="btnClear" type="Button" value="  C  " onClick="Clear()" class="button-style2"></TD>
   <TD><input name="btnClearEntry" type="Button" value="  CE " onClick="ClearEntry()" class="button-style2"></TD>
  </TR>
  <TR>
   <TD><input name="btnSeven" type="Button" value="  7  " onClick="NumPressed(7)" class="button-style"></TD>
   <TD><input name="btnEight" type="Button" value="  8  " onClick="NumPressed(8)" class="button-style"></TD>
   <TD><input name="btnNine" type="Button" value="  9  " onClick="NumPressed(9)" class="button-style"></TD>
   <TD></TD>
   <TD><input name="btnNeg" type="Button" value=" +/- " onClick="Neg()" class="button-style2"></TD>
   <TD><input name="btnPercent" type="Button" value="  %  " onClick="Percent()" class="button-style2"></TD>
  </TR>
  <TR>
   <TD><input name="btnFour" type="Button" value="  4  " onClick="NumPressed(4)" class="button-style"></TD>
   <TD><input name="btnFive" type="Button" value="  5  " onClick="NumPressed(5)" class="button-style"></TD>
   <TD><input name="btnSix" type="Button" value="  6  " onClick="NumPressed(6)" class="button-style"></TD>
   <TD></TD>
   <TD align=middle><input name="btnPlus" type="Button" value="  +  " onClick="Operation('+')" class="button-style2"> </TD>
   <TD align=middle><input name="btnMinus" type="Button" value="  -  " onClick="Operation('-')" class="button-style2"></TD>
  </TR>
  <TR>
   <TD><input name="btnOne" type="Button" value="  1  " onClick="NumPressed(1)" class="button-style"></TD>
   <TD><input name="btnTwo" type="Button" value="  2  " onClick="NumPressed(2)" class="button-style"></TD>
   <TD><input name="btnThree" type="Button" value="  3  " onClick="NumPressed(3)" class="button-style"></TD>
   <TD></TD>
   <TD align=middle><input name="btnMultiply" type="Button" value="  *  " onClick="Operation('*')" class="button-style2"></TD>
   <TD align=middle><input name="btnDivide" type="Button" value="  /  " onClick="Operation('/')" class="button-style2"></TD>
  </TR>
  <TR>
   <TD><input name="btnZero" type="Button" value="  0  " onClick="NumPressed(0)" class="button-style"></TD>
   <TD><input name="btnDecimal" type="Button" value="  .  " onClick="Decimal()" class="button-style"></TD>
   <TD><input name="btnEquals" type="Button" value="  =  " onClick="Operation('=')" class="button-style"></TD>
   <TD></TD>
   <TD><input name="btnReturn" type="Button" value="确 定" onClick="confirm()" class="button-style2"></TD>
   <TD><input name="btnReturn" type="Button" value="取 消" onClick="cancel()" class="button-style2"></TD>
  </TR>
 </TABLE>
 </B>
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var FKeyPad = document.forms['Keypad'];
var Accum = "0";
var FlagNewNum = false;
var PendingOp = ""; 
//===============================================================================
//[] ȷ
//[]     str1 - һ
//   str2 - ڶ
//   type - 
//   precision - Сλ
//[÷ʽ]   longCount(str1,str2,type,precision);
//[ֵ]   
//===============================================================================
    function longCount(str1,str2,type) {
        var comma1 = 0;
        if (str1.indexOf(".")!=-1) {
            str1 = str1.replace(/0*$/,"");
            comma1 = str1.length - str1.indexOf(".")-1;
        }
        var comma2 = 0;
        if (str2.indexOf(".")!=-1) {
            str2 = str2.replace(/0*$/,"");
            comma2 = str2.length - str2.indexOf(".")-1;
        }
        str1 = str1.replace(/\./,"");
        str2 = str2.replace(/\./,"");
        var value,comma;
        if (type!="*") {
            if (comma1>comma2) {
                for (var i=0;i<comma1-comma2;i++) str2 += "0";
                comma = (type=="/")?0:comma1;
            }else {
                for (var i=0;i<comma2-comma1;i++) str1 += "0";
                comma = (type=="/")?0:comma2;
            }
        }else {
            comma = comma1 + comma2;
        }
        if (type=="+") {
            value = parseInt(str1,10) + parseInt(str2,10);
        }else if (type=="-") {
            value = parseInt(str1,10) - parseInt(str2,10);
        }else if (type=="*") {
            value = parseInt(str1,10) * parseInt(str2,10);
        }else if (type=="/") {
            value = parseInt(str1,10) / parseInt(str2,10);
        }
        value = String(value);
        if (comma>0) value = value.substring(0,value.length-comma)+"."+value.substring(value.length-comma,value.length);
        if (value.indexOf(".")!=-1)
            value = value.replace(/0*$/,"");
        return value;
    }
function NumPressed (Num) {
 if (FlagNewNum) {
  FKeyPad.ReadOut.value  = Num;
  FlagNewNum = false;
   }
 else {
  if (FKeyPad.ReadOut.value == "0")
   FKeyPad.ReadOut.value = Num;
  else
   FKeyPad.ReadOut.value += Num;
   }
}
function Operation (Op) {
 var Readout = FKeyPad.ReadOut.value;
 if (FlagNewNum && PendingOp != "=");
 else
 {
  FlagNewNum = true;
  if ( '+' == PendingOp || '-' == PendingOp || '/' == PendingOp || '*' == PendingOp)
   Accum = longCount(Accum,Readout,PendingOp);
  else
   Accum = Readout;
        FKeyPad.ReadOut.value = Accum;
        PendingOp = Op;
        //FKeyPad.ReadOut.focus();
        //FKeyPad.ReadOut.select();
 }
}
function Decimal () {
 var curReadOut = FKeyPad.ReadOut.value;
 if (FlagNewNum) {
  curReadOut = "0.";
  FlagNewNum = false;
   }
 else
 {
  if (curReadOut.indexOf(".") == -1)
   curReadOut += ".";
   }
 FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
 FKeyPad.ReadOut.value = "0";
 FlagNewNum = true;
}
function Clear () {
 Accum = "0";
 PendingOp = "";
 ClearEntry();
}
function Neg () {
    //alert(FKeyPad.ReadOut.value);
 FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,"-1","*");
}
function Percent () {
 FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,Accum,"*");
 FKeyPad.ReadOut.value = longCount(FKeyPad.ReadOut.value,100,"/");
}
function goReturn() {
    top.returnValue = FKeyPad.ReadOut.value;
    self.close();
}
function CheckOut() {
    var keyCode = window.event.keyCode;
    if (keyCode>=48 && keyCode<=57) {
  if (FlagNewNum) {
   FKeyPad.ReadOut.value  = "";
   //window.event.keyCode = null;
   FlagNewNum = false;
  }
        return true;
    }else if (keyCode==43 || keyCode==45 || keyCode==42 || keyCode==47 || keyCode==61) {
        Operation(String.fromCharCode(keyCode));
    }else if (keyCode==46) {//.
        if (FKeyPad.ReadOut.value.indexOf(".") == -1)
         return true;
    }else if (keyCode==13) goReturn();
    window.event.returnValue = false;
    return false;
}
// End -->
</SCRIPT>
<script language="javascript">
	
	function confirm() {
		var result=document.getElementById("ReadOut").value;
		window.parent.closeLayer(result,"amount");
		document.getElementById("ReadOut").value="0";
	}
	
	function cancel() {
		var result="";
		window.parent.closeLayer(result,"amount");
	}
	
window.parent.showLayer(window);
</script>
</body>
</html>

